package cn.e3mall.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.support.Parameter;

import cn.e3mall.common.pojo.EasyUIDataGridResult;
import cn.e3mall.common.utils.E3Result;
import cn.e3mall.pojo.TbItem;
import cn.e3mall.service.ItemService;

/**
 * 商品管理Controller
 * <p>Title: ItemController</p>
 * <p>Description: </p>
 * <p>Company: www.itcast.cn</p> 
 * @version 1.0
 */
@Controller
public class ItemController {

	@Autowired
	private ItemService itemService;
	
	@RequestMapping("/item/{itemId}")
	@ResponseBody
	public TbItem getItemById(@PathVariable Long itemId) {
		TbItem tbItem = itemService.getItemById(itemId);
		return tbItem;
	}
	
	@RequestMapping("/item/list")
	@ResponseBody
	public EasyUIDataGridResult getItemList(Integer page, Integer rows) {
		//调用服务查询商品列表
		EasyUIDataGridResult result = itemService.getItemList(page, rows);
		return result;
	}
	
	/**
	 * 商品添加功能
	 */
	@RequestMapping(value="/item/save", method=RequestMethod.POST)
	@ResponseBody
	public E3Result addItem(TbItem item, String desc) {
		E3Result result = itemService.addItem(item, desc);
		return result;
	}
	
	/**
	 * 商品删除功能
	 */
	@RequestMapping("/rest/item/delete")
	@ResponseBody
	public E3Result deleteItemList(long[] ids) {
		
		//当商品列表的id为空时，返回失败结果
		if( ids.length == 0 || ids == null ) {
			return new E3Result(500, "删除商品失败",null);
		}
		
		//删除商品
		E3Result result = itemService.deleteItemList(ids);
		
		return result;
	}
	
	/**
	 * 商品上架功能
	 */
	@RequestMapping("/rest/item/reshelf")
	@ResponseBody
	public E3Result reshelfItemList(long[] ids) {
		
		//当商品列表的id为空时，返回失败结果
		if( ids.length == 0 || ids == null ) {
			return new E3Result(500, "上架商品失败",null);
		}
		
		//删除商品
		E3Result result = itemService.reshelfItemList(ids);
		
		return result;
	}
	
	/**
	 * 商品下架功能
	 */
	@RequestMapping("/rest/item/instock")
	@ResponseBody
	public E3Result instockItemList(long[] ids) {
		
		//当商品列表的id为空时，返回失败结果
		if( ids.length == 0 || ids == null ) {
			return new E3Result(500, "下架商品失败",null);
		}
		
		//下架商品
		E3Result result = itemService.downshelfItemList(ids);
		
		return result;
	}
	
	/**
	 * 	编辑商品功能
	 */
	
//	@RequestMapping("/rest/item/query/item/desc/{id}")
//	public E3Result editById(@RequestParam("id") Long id) {
//		
//		//当id为空
//		if(id == null) {
//			return null;
//		}
//		
//		return null;
//		
//	}
}
